package com.color.admin.auth.controller;


import com.color.admin.auth.entity.Captcha;
import com.color.admin.auth.entity.CaptchaCheck;
import com.color.admin.auth.entity.LoginDto;
import com.color.admin.auth.service.AuthService;
import com.color.admin.auth.service.CaptchaService;
import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author HaiQiu
 * @date 2021/12/15
 */
@Tag(name = "auth-controller", description = "框架所有应用授权服务中心接口")
@RestController
@RequestMapping("/auth/authorization")
public class AuthController extends BaseController {

    @Autowired
    private AuthService authService;

    @Autowired
    private CaptchaService captchaService;

    @Log("用户登录")
    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public ResultData login(@Parameter(name = "登录对象实体", description = "默认账号密码：admin")
                            @RequestBody LoginDto loginDto) {
        return decide("登录成功", authService.login(loginDto));
    }

    @Log("退出登录")
    @Operation(summary = "退出登录")
    @PostMapping("/out")
    public ResultData loginOut(@RequestAttribute String userId) {
        authService.loginOut(userId);
        return decide("退出成功");
    }

    @Log("验证码获取")
    @Operation(summary = "验证码接口",description = "验证码获取接口")
    @PostMapping("/captcha/get")
    public ResultData captcha(@Parameter(name = "验证码参数")@RequestBody Captcha captcha) {
        Object o = captchaService.captcha(captcha);
        return decide(o);
    }

    @Log("验证码校验")
    @Operation(summary = "验证码校验",description = "验证码第一次校验接口")
    @PostMapping("/captcha/check")
    public ResultData check(@Parameter(name = "验证码校验参数")@RequestBody CaptchaCheck check) {
        Object o = captchaService.check(check);
        return decide(o);
    }

    @Log("根据客户端ID获取授权码")
    @Operation(summary = "授权码获取", description = "根据客户端ID获取授权码")
    @GetMapping("/code")
    public ResultData getCode(@Parameter(name = "客户端ID") @RequestParam String clientId,
                              @Parameter(name = "重定向地址") @RequestParam String redirectUri) {
        Map<String, String> map = new HashMap<>(6);
        map.put("code", UUID.randomUUID().toString());
        map.put("clientId", clientId);
        map.put("redirectUri", "https://www.baidu.com");
        map.put("scope", "read");
        return decide(map);
    }

    @Log("授权码登录")
    @Operation(summary = "授权码登录", description = "使用获取的授权码进行登录获取令牌信息")
    @GetMapping("/login/code")
    public ResultData loginByCode(@Parameter(name = "客户端ID") @RequestParam String clientId,
                                  @Parameter(name = "授权码") @RequestParam String code) {
        Map<String, String> map = new HashMap<>(6);
        map.put("token", UUID.randomUUID().toString());
        return decide("授权码校验成功", map);
    }
}
